package com.example.smartcity_poverty.ui.AllService.StopWhere

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.ui.AllService.StopWhere.adapter.StopListAdapter
import com.example.smartcity_poverty.ui.SmartSubject.AllSubjectActivity.PovertyActivity.fragment.poverty.adapter.PtMoreAdapter
import com.example.smartcity_poverty.ui.SmartSubject.AllSubjectActivity.PovertyActivity.fragment.poverty.model.PtMoreEntity
import kotlinx.android.synthetic.main.activity_stop_where.*

/**
 * 停哪儿
 */
class StopWhereActivity : AppCompatActivity() {

    private val mVM by lazy {
        ViewModelProviders.of(this)[StopWhereViewModel::class.java]
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_stop_where)
        title = "停哪儿"

        // 服务
        setMore()

        // 列表适配器
        setList()
    }

    private fun setList() {

        mVM.stopWhereLiveData.observe(this, Observer {
            val body = it.getOrNull()
            if (body != null){
                mVM.stopWhereList.clear()
                mVM.stopWhereList.addAll(body.rows!!)

                stopWhereTwoRv.apply {
                    this.layoutManager = LinearLayoutManager(context)
                    this.adapter = StopListAdapter(mVM.stopWhereList, R.layout.re_stop_list)
                }
            }
        })


    }

    private fun setMore() {
        stopWhereOneRv.apply {
            this.layoutManager = GridLayoutManager( context, 1)
            this.adapter = PtMoreAdapter(ArrayList<PtMoreEntity>().apply {
                this.add(PtMoreEntity(R.mipmap.po2, "停车记录"))
            }, R.layout.re_item_poverty_more)
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}